/*
 * Equipe: Menandro Dias E. Neto
 *         Thiago Torres
 *         Ubirajara Gabriela
 */

package com.mapdelivery.model;

import com.mapdelivery.beans.MapDeliveryBean;
import com.mapdelivery.beans.Categoria;
import com.mapdelivery.model.dao.DAOCategoria;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author menandro.neto
 */
public class ModelCategoria implements ModelProxy {

    private ArrayList<MapDeliveryBean> categorias;
    private DAOCategoria daoCategoria;

    public ModelCategoria() {
        this.categorias = new ArrayList<MapDeliveryBean>();
        this.daoCategoria = new DAOCategoria();
    }

    public Integer count(){
        return this.categorias.size();
    }

    public void virtualDataProducer(){
        this.makeTestData();
    }

    /**
     *
     * @return Sucess when deleted
     */
    public Boolean delete(Integer pos) {
        Categoria bean = (Categoria) this.categorias.get(pos);
        this.daoCategoria.delete( bean.getCategoriaId() );
        return this.categorias.remove(pos);
    }

    /**
     *
     * @return Sucess when inserted
     */
    public Boolean insert(MapDeliveryBean item) {
        this.daoCategoria.insert(item);
        return this.categorias.add( (Categoria) item);
    }

    /**
     *
     * @return Sucess when updated
     */
    public Boolean update(Integer pos, MapDeliveryBean item) {
        Boolean ret;
        ret = ( pos.intValue() < 0 || pos.intValue() >= this.categorias.size() );
        if (ret)
                this.categorias.set(pos.intValue(), (Categoria) item);

        Categoria bean = (Categoria) this.categorias.get(pos);
        this.daoCategoria.update(bean.getCategoriaId(), item);

        return ret;
    }

    /**
     *
     * @return ArrayList<MapDeliveryBean>
     */
    public ArrayList<MapDeliveryBean> list() {
        this.categorias.removeAll(categorias);
        this.categorias = this.daoCategoria.findAll();
        return( this.categorias );
    }

    private void makeTestData(){
        this.categorias = this.daoCategoria.findAll();
        /*
        int i = 1;
        Categoria test;
        for (i = this.count(); i < 15; i++) {
            test = new Categoria();

            test.setCategoriaId(i + 1);
            test.setCatDescricao( "Categoria ".concat( String.valueOf(i) ) );
            
            this.insert(test);
        }
        */
    }
}
